package com.awspaas.user.apps.processforsap.event;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.ExecuteListener;
import com.actionsoft.bpms.bpmn.engine.listener.ListenerConst;
import com.actionsoft.sdk.local.SDK;

import java.sql.Connection;
import java.util.List;

public class FormDeleteAfterForMM extends ExecuteListener {
    @Override
    public void execute(ProcessExecutionContext param) throws Exception {
        //参数获取
        //注意：除特殊说明外，下列参数仅在该事件中场景有效
        //子表单项ID
        String boName = param.getParameterOfString(ListenerConst.FORM_EVENT_PARAM_BONAME);

        if ("BO_EU_SAP_JBST".equals(boName)){
            //被删除的记录，可读取该信息，进行删除后的补偿操作
            BO bo = (BO) param.getParameter(ListenerConst.FORM_EVENT_PARAM_REMOVED_BO);

            //注意：由于使用了事务，操作数据库需要使用如下方式获取的Connection连接
            //该参数仅在表单子表记录删除前（后）有效
            Connection conn = (Connection) param.getParameter(ListenerConst.FORM_EVENT_PARAM_CONNECTION);

            //执行删除后的业务补偿
            String id = bo.getId();
            //删除其他数据
            String[] arraName = {"BO_EU_SAP_FLST","BO_EU_SAP_XSST","BO_EU_SAP_CGST","BO_EU_SAP_CCST","BO_EU_SAP_CWST","BO_EU_SAP_MRP","BO_EU_SAP_GZJH"};
            for (String girdBoName:arraName){
                List<BO> list = SDK.getBOAPI().query(girdBoName).addQuery("MAINID=", id).list();
                for (BO bo1:list){
                    if(bo1!=null){
                        String newId = bo1.getId();
                        SDK.getBOAPI().remove(girdBoName,newId);
                    }
                }
            }
            List<BO> list = SDK.getBOAPI().query("BO_EU_SAP_PID").addQuery("MATERIAL=",bo.getString("MATERIAL")).list();
            for (BO bo1:list){
                SDK.getBOAPI().remove("BO_EU_SAP_PID",bo1.getId());
            }
        }else{

        }
    }
}
